<?php
$id		= JRequest::getInt('id', 0, 'GET');

$document=& JFactory::getDocument();
$user 		= &JFactory::getUser();
$db		= JFactory::getDbo();

$query	= $db->getQuery(true);
$query->select("*");
$query->from("#__content");
$query->where("id=".$id);
$db->setQuery($query);
$row = $db->loadObject();

$title = new JConfig();
$document->setTitle($title->sitename." - ".$row->title);

//attachment
$sql = $db->getQuery(true);
$sql->select("filename,url,display_name,description,icon_filename,access");
$sql->from("#__attachments");
$sql->where("parent_id=".$row->id." AND state=1");
$sql->order("id ASC");
$db->setQuery($sql);
$ats = $db->loadObjectList();

$sql = $db->getQuery(true);
$sql->select("id,title,created");
$sql->from("#__content");
$sql->where("id!=".$id." AND catid=".$row->catid);
$sql->order("ordering ASC, id DESC LIMIT 0, 10");
$db->setQuery($sql);
$rows = $db->loadObjectList();
?>
<div class="pagecontentheading"><?php echo $row->title;?></div>
<div class="page_date_time">Ngày cập nhật: <?php echo unFormatDate($row->created,"d/m/Y");?></div>
<div class="page_introtext">
	<?php echo $row->introtext;?>
	<?php
	if (count($ats)>0){
		echo '<p><b>File đính kèm:</b> ';
		foreach ($ats as $at) {
			if ($at->display_name!='') $name_file = $at->display_name; else $name_file = $at->filename;
			if ($user->id>0 && $at->access == 2){
				echo '<a style="color:#b73836; padding-right:10px;" href="'.$this->baseurl."/".$at->url.'">'.$name_file.'</a>';
			}
			if($at->access == 1){
				echo '<a style="color:#b73836; padding-right:10px;" href="'.$this->baseurl."/".$at->url.'">'.$name_file.'</a>';
			}
		}
		echo '</p>';
	} 
	//Create by
	$query	= $db->getQuery(true);
	$query->select("name");
	$query->from("#__users");
	$query->where("id=".$row->created_by);
	$db->setQuery($query);
	$user = $db->loadObject();
	?>
</div>
<div class="clearfix"></div>
<div class="page_fulltext">
	<?php echo $row->fulltext;?>
	<p align="right" style="color:#999;">(Được đăng bởi: <b><?php echo $user->name;?></b>)</p>
</div>
<div class="blog_more">
	<div><span>Các bài đã đăng</span></div>
	<ul>
		<?php
		$nows = mktime(0,0,0,date("m"),date("d"),date("Y"));
		for ($i=0; $i<count($rows); $i++){
			$ngay_nhap = mktime(0,0,0,unFormatdate($rows[$i]->created,"m"),unFormatdate($rows[$i]->created,"d"),unFormatdate($rows[$i]->created,"Y"));				
			$days = ($nows - $ngay_nhap)/86400;
			if ($days>0){
				$class = '';
			}else{
				$class = ' class="icon_news"';
			}
			$link = JRoute::_("index.php?option=com_content&view=article&id=".$rows[$i]->id);
			//attachment
			$sql = $db->getQuery(true);
			$sql->select("filename,url,display_name,description,icon_filename,access");
			$sql->from("#__attachments");
			$sql->where("parent_id=".$rows[$i]->id." AND state=1");
			$sql->order("id ASC");
			$db->setQuery($sql);
			$ats = $db->loadObjectList();
			echo '<li><a '.$class.'href="'.$link.'">'.$rows[$i]->title.'</a>';
			if (count($ats)>0){
				foreach ($ats as $at) {
					if ($at->display_name!='') $name_file = $at->display_name; else $name_file = $at->filename;
					if ($user->id>0 && $at->access == 2){
						echo ' (<a style="color:#b73836;" href="'.$this->baseurl."/".$at->url.'">'.$name_file.'</a>)';
					}
					if($at->access == 1){
						echo ' (<a style="color:#b73836;" href="'.$this->baseurl."/".$at->url.'">'.$name_file.'</a>)';
					}
				}
			}
			echo '</li>'; 
			echo '<span class="clearfix"></span>';
		} 
		?>
	</ul>
</div>
<script>
$(document).ready(function(){
	$('.page_introtext img').hide();
});
</script>